package com.greenant.store.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.greenant.store.R;
import com.greenant.store.utils.Bimp;
import com.greenant.store.utils.CommonAdapter;
import com.greenant.store.utils.ViewHolder;

public class MyAdapter extends CommonAdapter<String>
{

	/**
	 * 閻€劍鍩涢柅澶嬪閻ㄥ嫬娴橀悧鍥风礉鐎涙ê鍋嶆稉鍝勬禈閻楀洨娈戠�瑰本鏆ｇ捄顖氱窞
	 */
	public static List<String> mSelectedImage = new ArrayList<String>();
	
	public static final int maxGridCount =1;

	/**
	 * 閺傚洣娆㈡径纭呯熅�?�帮�?
	 */
	private String mDirPath;

	private Handler mHandler;
	
	private int selectTotal = 0;
	
	public MyAdapter(Context context, List<String> mDatas, int itemLayoutId,
			String dirPath,Handler mHandler)
	{
		super(context, mDatas, itemLayoutId);
		this.mDirPath = dirPath;
		this.mHandler = mHandler;
	}

	@Override
	public void convert(final ViewHolder helper, final String item)
	{
		//鐠佸墽鐤唍o_pic
		helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
		//鐠佸墽鐤唍o_selected
				helper.setImageResource(R.id.id_item_select,
						R.drawable.picture_unselected);
		//鐠佸墽鐤嗛崶鍓у
		helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item);
		
		final ImageView mImageView = helper.getView(R.id.id_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);
		
		mImageView.setColorFilter(null);
		//鐠佸墽鐤咺mageView閻ㄥ嫮鍋ｉ崙璁崇皑娴狅拷
		mImageView.setOnClickListener(new OnClickListener()
		{
			//闁�?�ㄩ敍灞藉灟鐏忓棗娴�?悧鍥у綁閺嗘绱濋崣宥勭閸掓瑥寮芥稊锟�
			@Override
			public void onClick(View v)
			{
				Log.d("sgz","drr_size="+Bimp.drr.size()+",selectTotal="+selectTotal);
				if ((Bimp.drr.size() + selectTotal) < MyAdapter.maxGridCount) {					
					if (mSelectedImage.contains(mDirPath + "/" + item))
					{
						mSelectedImage.remove(mDirPath + "/" + item);
						mSelect.setImageResource(R.drawable.picture_unselected);
						mImageView.setColorFilter(null);
						selectTotal--;						
					} else
					{					
							selectTotal++;
							mSelectedImage.add(mDirPath + "/" + item);
							mSelect.setImageResource(R.drawable.pictures_selected);
							mImageView.setColorFilter(Color.parseColor("#77000000"));					
					}
				} else if ((Bimp.drr.size() + selectTotal) >= MyAdapter.maxGridCount) {
					if (mSelectedImage.contains(mDirPath + "/" + item))
					{
						mSelectedImage.remove(mDirPath + "/" + item);
						mSelect.setImageResource(R.drawable.picture_unselected);
						mImageView.setColorFilter(null);
						selectTotal--;											
					} else {
						Message message = Message.obtain(mHandler, 2);
						message.sendToTarget();
					}
				}
			}
		});
		
		/**
		 * 瀹歌尙绮￠柅澶嬪鏉╁洨娈戦崶鍓у閿涘本妯夌粈鍝勫毉闁�?�ㄦ潻鍥╂畱閺佸牊鐏�?
		 */
		if (mSelectedImage.contains(mDirPath + "/" + item))
		{
			mSelect.setImageResource(R.drawable.pictures_selected);
			mImageView.setColorFilter(Color.parseColor("#77000000"));
		}

	}

}
